package com.example.demo.controller;

import com.example.demo.pojo.Result;
import com.example.demo.pojo.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/addStudent")
    public Result addStudent(@RequestBody Student student) {
        studentService.insert(student);
        return Result.success();
    }

    @DeleteMapping("/deleteStudent/{id}")
    public Result deleteStudent(@PathVariable Integer id) {
        studentService.deleteById(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateStudent(@RequestBody Student student) {
        studentService.update(student);
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result<List<Student>> getAllStudents() {
        List<Student> students = studentService.selectAll();
        return Result.success(students);
    }

    @GetMapping("/select/{id}")
    public Result<Student> getStudentById(@PathVariable("id") Integer id) {
        Student student = studentService.selectById(id);
        return Result.success(student);
    }

    @RequestMapping("/selectByName")
    public Result<List<Student>> searchStudentsByNameKeyword(String keyword) {
        List<Student> student =studentService.selectByNameKeyword(keyword);
        return Result.success(student);
    }
}